package com.dot.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.dot.dao.BaseDao;
import com.dot.util.BeanUtils;
import com.dot.util.Page;

@Repository("baseDao")
public class BaseDaoImpl<T> implements BaseDao<T>{

	@Autowired()
	SqlSession sqlSession;
	
	public static final String SQLNAME_SEPARATOR = ".";
	
	private String sqlNamespace = getDefaultSqlNamespace();
	
	public static final Logger logger = Logger.getLogger(BaseDaoImpl.class);
	
	protected String getDefaultSqlNamespace() {
		Class<?> genericClass = BeanUtils.getGenericClass(this.getClass());
		return genericClass == null ? null : genericClass.getName();
	}
	
	protected String getSqlName(String sqlName) {
		return sqlNamespace + SQLNAME_SEPARATOR + sqlName;
	}
	
	public void save(T t) throws Exception{
		sqlSession.insert(getSqlName("save"), t);
	}

	public T getById(Long id) {
		return sqlSession.selectOne(getSqlName("get"), id);
	}

	public void remove(Long id) {
		sqlSession.delete(getSqlName("remove"), id);
	}

	public void update(T t) {
		sqlSession.update(getSqlName("update"), t);
	}

	public List<T> getAll() {
		return sqlSession.selectList(getSqlName("getAll"));
	}

	public Page<T> get(int pageNo, int pageSize, T q) {
		return null;
	}

	public List<T> getByObj(T t) throws Exception {
		return sqlSession.selectList(getSqlName("getByObj"), t);
	}
	

}
